/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import "github.com/mjiulee/lego"
import . "saas_sys_base/modules/system/models"

type SysUserService struct {
	// add your model here
	tbSysUser    SysUser
	tbSysProfiel SysUserProfile
}

func (t *SysUserService) ListSysUser(keyword string, page, psize int) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_sys_user", "alias": "t1"},
		"fields": []string{"t1.*", "t2.user_name", "t2.mobile", "t2.job_title", "t2.email"},
		"join": []map[string]string{
			map[string]string{"table": "tb_sys_user_profile as t2", "on": "t2.sys_uid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			//{"t1.if_del", "=", 0, "AND", false},
		},
		"search": map[string]string{
			"t1.user_name": keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}
	return lego.QueryListByMap(queryMap)
}

/* 根据id获取记录
 * -----------------
 */
func (t *SysUserService) EncryptPassword(pwd string) string {
	return t.tbSysUser.EncryptPassword(pwd)
}

/* 根据id获取记录
 * -----------------
 */
func (t *SysUserService) GetSysUserById(id int64) (*SysUser, *SysUserProfile) {
	return t.tbSysUser.RecodeById(id), t.tbSysProfiel.RecodeBySysUid(id)
}

/* 根据系统用户名获取记录
 * -----------------
 */
func (t *SysUserService) GetSysUserByAccount(account string) (*SysUser, *SysUserProfile) {
	sysUser := t.tbSysUser.RecodeByAccount(account)
	if sysUser == nil {
		return nil, nil
	}
	return sysUser, t.tbSysProfiel.RecodeBySysUid(sysUser.Id)
}

/* 添加
 * -----------------
 */
func (t *SysUserService) SaveSysUser(item2add *SysUser, profile *SysUserProfile) bool {
	return t.tbSysUser.AddRecode(item2add, profile)
}

/* 删除(硬删除)*/
func (t *SysUserService) DelSysUserById(id int64) bool {
	return t.tbSysUser.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *SysUserService) UpdateSysUser(rc *SysUser, profile *SysUserProfile, profileExist bool) bool {
	return t.tbSysUser.UpdateRecode(rc, profile, profileExist)
}
